<?php

declare (strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
 * 回合动作
 * Class RoundActionModel
 * @package App\Model
 */
class RoundActionModel extends Model
{


    protected $table = 'rounds_action';

    protected $guarded = [
        'id',
        'created_at',
        'updated_at',
    ];

    protected $casts = [
        'action_cards' => 'array',
        'remain_cards' => 'array',

        'banker_cards' => 'array',
        'prev_cards' => 'array',
        'next_cards' => 'array',
        'opposite_cards' => 'array',


        'banker_discard' => 'array',
        'prev_discard' => 'array',
        'next_discard' => 'array',
        'opposite_discard' => 'array',


        'banker_peng' => 'array',
        'prev_peng' => 'array',
        'next_peng' => 'array',
        'opposite_peng' => 'array',

        'banker_gang' => 'array',
        'prev_gang' => 'array',
        'next_gang' => 'array',
        'opposite_gang' => 'array',

        'is_end' => 'boolean',
    ];

    /**
     * 当前动作操作用户
     * @return \Hyperf\Database\Model\Relations\BelongsTo
     */
    public function User()
    {
        return $this->belongsTo(UsersModel::class, 'uid');
    }

    /**
     * 房间
     * @return \Hyperf\Database\Model\Relations\BelongsTo
     */
    public function Room()
    {
        return $this->belongsTo(RoomsModel::class, 'rooms_id');
    }

    /**
     * 回合
     * @return \Hyperf\Database\Model\Relations\BelongsTo
     */
    public function Round()
    {
        return $this->belongsTo(RoundsModel::class, 'rounds_id');
    }

    /**
     * 获取当前动作的值
     * @return int|null
     */
    public function getActionVal(): ?int
    {
        return $this->action_cards[0] ?? null;
    }

    /**
     * 获取当前动作发起者的手牌
     * @return array
     */
    public function getCurrentCards(): ?array
    {
        return $this->{"{$this->position}_cards"} ?? [];
    }
}